<template>
  <div id="main"></div>
</template>

<script>
export default {
  props: {
    goodsCategoryData: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    goodsCategoryData(newValue) {
      this.$props.goodsCategoryData = newValue;
      this.mapEchart();
    },
  },
  mounted() {
    this.mapEchart();
  },
  methods: {
    mapEchart() {
      var chartDom = document.getElementById("main");
      var myChart = this.$echarts.init(chartDom);
      var option;

      const newData = this.$props.goodsCategoryData.map((item) => ({
        name: item.name,
        value: item.count,
      }));

      option = {
        title: {
          text: "惠选产品分类",
          left: "12",
          top: "8",
          textStyle: {
            color: "#fff",
            fontSize: 20,
          },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          right: 10,
          top: 6,
          textStyle: {
            color: "#fff",
            lineHeight: 18,
          },
        },
        series: [
          {
            name: "惠选产品分类",
            type: "pie",
            radius: "50%",
            data: newData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            right: 60,
            top: 0,
            label: {
              normal: {
                textStyle: {
                  color: "#fff",
                },
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);

      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
  },
};
</script>

<style scoped>
#main {
  width: 100%;
  height: 100%;
}
</style>
